
var Supplier = {
	
	supplier_x: function(form) {
		
		var supplier_x = {};				
		supplier_x.name = form.c_name.value;
		supplier_x.fullname = form.c_fullname.value;
		supplier_x.address = form.c_address.value;				
		supplier_x.web = form.c_web.value;
		
		supplier_x.contact = form.c_contact.value;
		supplier_x.mobile = form.c_mobile.value;
		supplier_x.tel = form.c_tel.value;
		supplier_x.fax = form.c_fax.value;
		supplier_x.email = form.c_email.value;
		supplier_x.content = form.c_content.value;		
		
		return $.toJSON(supplier_x);
	},
	
	linkman: function (a) {		
		$select = $("#supplier_linkman");		
		$select.empty();
		count = 0;
		$.each(a, function(k, v) {
	    	var item = new Option(v.name, v.id);
	    	$select[0].options.add(item);
	    	
	    	if (count == 1) {
	    		Supplier._link_man(v.id, false);
	    	}
	    	
	    	count++;
	    });
		
		$select.change(function() {
			var id = $(this).val();
			Supplier._link_man(id, true);
		}); 
	},

	_link_man: function (id, select) {
		var url = 'info.php?do=Contact.ajax_id&q=' + id;
		$.getJSON(url, function(json) {
			var aaa = Supplier.aaa();
			if (select || aaa) {
				$("#c_contact").val(json.name);
				$("#c_mobile").val(json.mobile);
				$("#c_tel").val(json.tel);
				$("#c_fax").val(json.fax);
				$("#c_email").val(json.email);
			}
		});
	},
	
	aaa: function() {
		if ($("#c_contact").val().trim() != "") {
			return false;
		}
		 
		if ($("#c_mobile").val().trim() != "") {
			return false;
		} 
		
		if ($("#c_tel").val().trim() != "") {
			return false;
		}
		
		if ($("#c_fax").val().trim() != "") {
			return false;
		}
		
		if ($("#c_email").val().trim() != "") {
			return false;
		}
		
		return true;
	}
};